<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class GoodsController extends Controller
{
    //商品主页
    public function index(){
        $goods = DB::table('goods')->paginate(4);
        //var_dump($goods);
        return view('/admin/goods/index',['goods'=>$goods]);
    }

    //添加商品页面
    public function add(){
        return view('/admin/goods/add');
    }

    //添加商品操作
    public function insert(Request $request){
        //检测商品名是否存在
        if($request->input('gname') == '' || $request->input('price') == '' || $request->input('gname') == '' ){
            return error('/admin/goods/add','对不起,您有数据没有填写',3);
        }

        //检测是否上传文件
        if(!$request->hasFile('img')){
            return error('/admin/goods/add','对不起,您未上传图片,请上传图片',3);
        }

        //生成文件夹
        $dir = date('Y-m-d');
        //生成文件名
        $fileName = uniqid().'.jpg';
        //组合上传路径存储到数据库中
        $path = $dir.'/'.$fileName;
        //移动上传文件
        $request->file('img')->move($dir,$fileName);

        $goods = $request->only('gname','price','num','info','cid');
        $goods['img'] = $path;
        
        //var_dump($goods);
        //插入数据库
        $result = DB::table('goods')->insert($goods);

        if($result){
            //操作成功 跳转到商品列表页面
            return redirect('/admin/goods/index');
        }else{
            //操作失败 跳转提醒页面
            return error('/admin/goods/add','添加商品失败,请重新填写',3);
        }
    }

    //删除商品操作
    public function delete(Request $request){
        $gid = $request->input('gid');
        //var_dump($gid);

        $result = DB::table('goods')->where('gid','=',$gid)->delete();

        if($result){
            return 1;  
        }else{
            return 0;
        }
    }

    //更新商品的方法
    public function modify(Request $request){
        $gid = $request->input('gid');

        //查询商品信息
        $good = DB::table('goods')->where('gid','=',$gid)->first();
        
        //json编码返回
        return response()->json($good); 
    }

    
    //更新商品列表操作
    public function update(Request $request){
        //var_dump($request);
        //判断用户是否更改图片
        if(!$request->hasFile('img')){
            //var_dump($request->input('yimg'));
            $goods = $request->only('gname','price','num','cid','info');
            $goods['img']= $request->input('yimg');
            //var_dump($goods); 

        }else{
            
            //生成文件夹
            $dir = date('Y-m-d');
            //生成文件名
            $fileName = uniqid().'.jpg';
            //组合上传路径存储到数据库中
            $path = $dir.'/'.$fileName;
            //var_dump($path);
            //移动上传文件
            $request->file('img')->move($dir,$fileName);

            $goods = $request->only('gname','price','num','info','cid');
            $goods['img'] = $path;
            //var_dump($goods);
            
        }

        $gid = $request->input('gid');
        //插入数据库
        $result = DB::table('goods')->where('gid','=',$gid)->update($goods);

        if($result){
            return success('/admin/goods/index','更改商品成功',1);
        }else{
            return error('/admin/goods/add','更改商品失败',2);
        }
        
    }
    

    //查找商品界面
    public function find(){
        //跳转到查找商品界面
        return view('/admin/goods/find');
    }

    //查询商品操作
    public function search(Request $request){
        //接收关键字
        $keyword = $request->input('key');
        //var_dump($keyword);
        $allGoods = DB::table('goods')
                            ->where('gid','=',$keyword)
                            ->orwhere('gname','like','%'.$keyword.'%')
                            ->orwhere('cid','like','%'.$keyword.'%')
                            ->paginate(4);
        //var_dump($allGoods);                
        //将查找的商品分配到商品模板中
        return view('/admin/goods/index',['goods'=>$allGoods]);

    }

}
